package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author ：seanyang
 * @date ：Created in 2019/7/2
 * @description ：业务报表实现类
 * @version: 1.0
 */
@Service
public class ReportServiceImpl implements ReportService {
	@Autowired
	private MemberDao memberDao;

	@Autowired
	private OrderDao orderDao;

	@Override
	public Map<String, Object> geTime(String starttime, String endtime) {
		String start=starttime+"-1";
		String end=endtime+"+31";

		return null;
	}

	/**
	 * reportDate:null,
	 * todayNewMember :0,
	 * totalMember :0,
	 * thisWeekNewMember :0,
	 * thisMonthNewMember :0,
	 * todayOrderNumber :0,
	 * todayVisitsNumber :0,
	 * thisWeekOrderNumber :0,
	 * thisWeekVisitsNumber :0,
	 * thisMonthOrderNumber :0,
	 * thisMonthVisitsNumber :0,
	 * hotSetmeal:[]
	 * @return
	 */
	@Override
	public Map<String, Object> getBusinessReportData() {
		Map<String,Object> resultMap = new HashMap<>();
		try{
			// 获取今日
			String today = DateUtils.parseDate2String(DateUtils.getToday());
			// 获取本周第1天
			String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
			// 获取本月第1天
			String thisFirstDayInMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
			// 报表日期
			resultMap.put("reportDate",today);
			// 获取会员相关数据
			// * todayNewMember :0, totalMember :0, thisWeekNewMember :0, thisMonthNewMember :0,
			resultMap.put("todayNewMember",memberDao.findMemberCountByDate(today));
			resultMap.put("totalMember",memberDao.findMemberTotalCount());
			resultMap.put("thisWeekNewMember",memberDao.findMemberCountAfterDate(thisWeekMonday));
			resultMap.put("thisMonthNewMember",memberDao.findMemberCountAfterDate(thisFirstDayInMonth));
			/**
			 *  todayOrderNumber :0,
			 *  todayVisitsNumber :0,
			 *  thisWeekOrderNumber :0,
			 *  thisWeekVisitsNumber :0,
			 *  thisMonthOrderNumber :0,
			 *  thisMonthVisitsNumber :0,
			 */
			// 预约相关数据
			resultMap.put("todayOrderNumber",orderDao.findOrderCountByDate(today));
			resultMap.put("thisWeekOrderNumber",orderDao.findOrderCountByAfterDate(thisWeekMonday));
			resultMap.put("thisMonthOrderNumber",orderDao.findOrderCountByAfterDate(thisFirstDayInMonth));
			// 到诊相关数据
			resultMap.put("todayVisitsNumber",orderDao.findVisitsCountByDate(today));
			resultMap.put("thisWeekVisitsNumber",orderDao.findVisitsCountByAfterDate(thisWeekMonday));
			resultMap.put("thisMonthVisitsNumber",orderDao.findVisitsCountByAfterDate(thisFirstDayInMonth));
			// 获取热门数据 hotSetmeal:[]
			List<Map> list = orderDao.findHotSetmeal();
			System.out.println(list.size());
			resultMap.put("hotSetmeal",list);
		}catch(Exception e){
		    e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 返回饼形图数据的实现
	 * @return
	 */
	@Override
	public Map<Object,Object> findBirthday() {

		//获取生日
		List<Date> DateList =   memberDao.findBirthday();
		//0-18
		List<String> youngList = new ArrayList<>();
		//18-30
		List<String> youthList = new ArrayList<>();
		//30-45
		List<String> middleList = new ArrayList<>();
		//45<
		List<String> oldList = new ArrayList<>();




		for (Date oneDate : DateList) {
			Integer age = DateUtils.getAge(oneDate);
			if (age <=18) {
				youngList.add(age + "");
			}

			if (age > 18 && age <= 30) {
				youthList.add(age + "");
			}

			if (30 < age && age <= 45) {
				middleList.add(age + "");
			}
			if (age > 45) {
				oldList.add(age + "");

			}
		}
		//要给前端返回的键名
		List<String> nameList = new ArrayList<>();
		nameList.add("0-18");
		nameList.add("18-30");
		nameList.add("30-45");
		nameList.add("45以上");

		Map<String, Object> youngMap = new HashMap<>();
		youngMap.put("name", "0-18");
		youngMap.put("value", youngList.size());


		Map<String, Object> youthMap = new HashMap<>();
		youthMap.put("name", "18-30");
		youthMap.put("value", youthList.size());


		Map<String, Object> middleMap = new HashMap<>();
		middleMap.put("name", "30-45");
		middleMap.put("value", middleList.size());

		Map<String, Object> oldMap = new HashMap<>();
		oldMap.put("name", "45以上");
		oldMap.put("value", oldList.size());


		List<Map<String, Object>> mapList = new ArrayList<>();
		mapList.add(youngMap);
		mapList.add(youthMap);
		mapList.add(middleMap);
		mapList.add(oldMap);



		//最终带回去的数据
		HashMap<Object, Object> mapData = new HashMap<>();
		mapData.put("nameList", nameList);
		mapData.put("mapList", mapList);
		return mapData;

	}

}
